package com.zxy.ziems.server.utils;

import com.zxy.ziems.server.device.pojo.bo.Coordinate;
import com.zxy.ziems.server.scene.pojo.bo.Frame;
import com.zxy.ziems.server.scene.pojo.entity.SceneEntity;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * @author 10348359
 * @create 2024-03-15 16:19
 */
public class CoordinateUtil {

    // 传入的xy坐标都是相对左下角点的相对坐标
    public static Coordinate calculteCoordinate(Coordinate coordinate, SceneEntity scene) {
        if (Objects.isNull(coordinate)) {
            return Coordinate.ZERO;
        }

        if (Objects.isNull(scene)) {
            return Coordinate.ZERO;
        }

        if (BigDecimal.ZERO.compareTo(scene.getWidth()) == 0 || BigDecimal.ZERO.compareTo(scene.getHeight()) == 0) {
            return Coordinate.ZERO;
        }

        // 坐标点
        BigDecimal x = coordinate.getX();
        BigDecimal y = coordinate.getY();
        BigDecimal z = coordinate.getZ();

        // 计算x坐标
        BigDecimal realX = x.divide(scene.getWidth(), RoundingMode.HALF_UP).multiply(scene.getFrameWidth()).add(scene.getFrameX());
        // 计算y坐标
        BigDecimal realY = y.divide(scene.getHeight(), RoundingMode.HALF_UP).multiply(scene.getFrameHeight()).add(scene.getFrameY());
        BigDecimal realZ = z.add(scene.getFrameZ());

        return new Coordinate(realX, realY, realZ);
    }
}
